package com.bawei.module_home.ui.visitor

import android.app.DatePickerDialog
import android.app.TimePickerDialog
import android.content.Context
import androidx.activity.viewModels
import com.alibaba.android.arouter.facade.annotation.Route
import com.bawei.lib_common.Router
import com.bawei.lib_common.base.BaseActivity
import com.bawei.lib_common.base.BaseViewModel
import com.bawei.lib_common.base.state.Empty
import com.bawei.lib_common.base.state.Failed
import com.bawei.lib_common.base.state.Loading
import com.bawei.lib_common.base.state.Success
import com.bawei.lib_common.observeWithLifecycle
import com.bawei.lib_common.utils.ToastUtils
import com.bawei.lib_storage.AppMMKV
import com.bawei.module_home.R
import com.bawei.module_home.databinding.ActivityVisitorBinding
import com.bawei.module_home.http.HomeServiceViewModel
import com.bawei.module_home.model.VisitorBody
import dagger.hilt.android.AndroidEntryPoint
import java.util.Calendar

@AndroidEntryPoint
@Route(path = Router.VISITOR_PAGE)
class VisitorActivity : BaseActivity<BaseViewModel,ActivityVisitorBinding>() {
    override val mLayoutResId: Int
        get() = R.layout.activity_visitor
    private val mViewModel: HomeServiceViewModel by viewModels()
    var state:Boolean=false
    override fun initView() {
        super.initView()
        mBinding.lfrq.setOnClickListener {
            showDatePicker(this) { year, month, day ->
                val date = "$year-${month + 1}-$day"
                mBinding.lfrq.setText("$date")
            }
        }
        mBinding.start.setOnClickListener {
            showTimePicker(this) { hour, minute ->
                val time = "$hour:$minute"
                mBinding.start.setText("$time")
            }
        }
        mBinding.end.setOnClickListener {
            showTimePicker(this) { hour, minute ->
                val time = "$hour:$minute"
                mBinding.end.setText("$time")
            }
        }
        mBinding.open.setOnClickListener {
            state=!state
        }
        mBinding.back.setOnClickListener {
            finish()
        }
    }
// 日期选择器
fun showDatePicker(context: Context, onDateSelected: (year: Int, month: Int, day: Int) -> Unit) {
    val calendar = Calendar.getInstance()
    val year = calendar.get(Calendar.YEAR)
    val month = calendar.get(Calendar.MONTH)
    val day = calendar.get(Calendar.DAY_OF_MONTH)

    val datePickerDialog = DatePickerDialog(
        context,
        { _, selectedYear, selectedMonth, selectedDay ->
            onDateSelected(selectedYear, selectedMonth, selectedDay)
        },
        year,
        month,
        day
    )
    datePickerDialog.show()
}

    // 时间选择器
    fun showTimePicker(context: Context, is24HourView: Boolean = true, onTimeSelected: (hour: Int, minute: Int) -> Unit) {
        val calendar = Calendar.getInstance()
        val hour = calendar.get(Calendar.HOUR_OF_DAY)
        val minute = calendar.get(Calendar.MINUTE)

        val timePickerDialog = TimePickerDialog(
            context,
            { _, selectedHour, selectedMinute ->
                onTimeSelected(selectedHour, selectedMinute)
            },
            hour,
            minute,
            is24HourView
        )
        timePickerDialog.show()
    }
    override fun initData() {
        mBinding.btnYes.setOnClickListener {
            if(mBinding.etId.text.isEmpty()||mBinding.etName.text.isEmpty()||
                mBinding.etPhone.text.isEmpty()||mBinding.lfrq.text.isEmpty()||mBinding.start.text.isEmpty()||
                mBinding.end.text.isEmpty()||mBinding.cph.text.isEmpty()||mBinding.fwdx.text.isEmpty()){
                ToastUtils.showLong(this,"请填写完整信息")
                return@setOnClickListener
            }
            val body = VisitorBody(
                appointmentTime=mBinding.lfrq.text.toString(),
                approved=true,
                attachments= listOf("1"),
                employeeId=1,
                id=AppMMKV.userId,
                idNumber=mBinding.etId.text.toString(),
                phoneNumber=mBinding.etPhone.text.toString(),
                plateNumber=mBinding.cph.text.toString(),
                purpose=mBinding.dfmd.text.toString(),
                requestParking=true,
                visitStatus="待审核",
                visitorName=mBinding.etName.text.toString()
            )
            mViewModel.addVisitor(body)
            mBinding.lfrq.setText("")
            mBinding.start.setText("")
            mBinding.end.setText("")
            mBinding.cph.setText("")
            mBinding.dfmd.setText("")
            mBinding.etId.setText("")
            mBinding.etName.setText("")
            mBinding.etPhone.setText("")
            mBinding.fwdx.setText("")
        }
    }

    override fun initListener() {
        mViewModel.addVisitor.observeWithLifecycle(this){
            when(it){
                Empty -> {

                }
                is Failed -> {
                    ToastUtils.showLong(this,"保存失败")
                }
                Loading -> {}
                Success -> {
                    ToastUtils.showLong(this,"保存成功")
                    //李乙甲
                }
            }
        }
    }

}